package system

import (
	"gove/admin/schemas/req"
	"gove/admin/service/system"
	"gove/core"
	"gove/core/request"
	"gove/core/response"
	"gove/middleware"
	"gove/util"

	"github.com/gin-gonic/gin"
)

var DictGroup = core.Group("/dict", newDictHandler, regDict, middleware.TokenAuth())

func newDictHandler(srv system.IDictService) *dictHandler {
	return &dictHandler{srv: srv}
}

func regDict(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *dictHandler) {
		rg.GET("/all", handle.all)
		rg.GET("/list", handle.list)
		rg.GET("/:id/form", handle.detail)
		rg.POST("/add", handle.add)
		rg.POST("/edit", handle.edit)
		rg.POST("/del/:ids", handle.del)
		rg.GET("/group/all", handle.GroupAll)
		rg.GET("/group/list", handle.GroupList)
		rg.GET("/group/:id/form", handle.GroupDetail)
		rg.POST("/group/add", handle.GroupAdd)
		rg.POST("/group/edit", handle.GroupEdit)
		rg.DELETE("/group/del/:ids", handle.GroupDel)
	})
}

type dictHandler struct {
	srv system.IDictService
}

// all 字典数据所有
func (ddh dictHandler) all(c *gin.Context) {
	var allReq req.DictListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &allReq)) {
		return
	}
	res, err := ddh.srv.All(allReq)
	response.CheckAndRespWithData(c, res, err)
}

// list 字典数据列表
func (ddh dictHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.DictListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := ddh.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail 字典数据详情
func (ddh dictHandler) detail(c *gin.Context) {
	var detailReq req.DictDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := ddh.srv.Detail(detailReq.ID)
	response.CheckAndRespWithData(c, res, err)
}

// detail 字典数据新增
func (ddh dictHandler) add(c *gin.Context) {
	var addReq req.DictAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, ddh.srv.Add(addReq))
}

// edit 字典数据编辑
func (ddh dictHandler) edit(c *gin.Context) {
	var editReq req.DictEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, ddh.srv.Edit(editReq))
}

// del 字典数据删除
func (ddh dictHandler) del(c *gin.Context) {
	var delReq req.DictDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, ddh.srv.Del(delReq))
}

// DictGroup结构体方法

// all 字典类型所有
func (dth dictHandler) GroupAll(c *gin.Context) {
	res, err := dth.srv.GroupAll()
	response.CheckAndRespWithData(c, res, err)
}

// list 字典类型列表
func (dth dictHandler) GroupList(c *gin.Context) {
	var page request.PageReq
	var listReq req.DictGroupListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := dth.srv.GroupList(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail 字典类型详情
func (dth dictHandler) GroupDetail(c *gin.Context) {
	var detailReq req.DictGroupDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := dth.srv.GroupDetail(detailReq.ID)
	response.CheckAndRespWithData(c, res, err)
}

// detail 字典类型新增
func (dth dictHandler) GroupAdd(c *gin.Context) {
	var addReq req.DictGroupAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, dth.srv.GroupAdd(addReq))
}

// edit 字典类型编辑
func (dth dictHandler) GroupEdit(c *gin.Context) {
	var editReq req.DictGroupEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, dth.srv.GroupEdit(editReq))
}

// del 字典类型删除
func (dth dictHandler) GroupDel(c *gin.Context) {
	var delReq req.DictGroupDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, dth.srv.GroupDel(delReq))
}
